<?php 
namespace Cmf\Forms;

use Phalcon\Forms\Form;
use Phalcon\Forms\Element\Text;
use Phalcon\Forms\Element\Submit;
use Phalcon\Forms\Element\Hidden;
use Phalcon\Validation\Validator\PresenceOf;

/**
* 角色表单
*/
class RoleForm extends Form
{
	public function initialize($role,$action = null)
	{
		if(!empty($role) && $action === 'edit'){
			$id = new Hidden('id');
			$this->add($id);
		}

		$title = new Text('title',['placeholder'=>'请输入角色名称']);
		$title->addValidators([new PresenceOf(['message'=>'角色名称不能为空'])]);
		$title->setLabel('角色名称');
		$this->add($title);

		$description = new Text('description',['placeholder'=>'请输入角色描述']);
		$description->addValidators([new PresenceOf(['message'=>'角色描述不能为空'])]);
		$description->setLabel('角色描述');
		$this->add($description);

		$status = new Hidden('status',['value'=>1]);
		$this->add($status);

		$submit = new Submit('submit',['value'=>'保存修改','class'=>'am-btn am-btn-primary']);
		$this->add($submit);
	}

	public function renderDecorated($name)
	{
		$element = $this->get($name);
		$messages = $this->getMessagesFor($element->getName());

		if(!in_array($element->getName(),['id','role_id'])) {
			$input = '<div class="am-form-group">';
			$input .= '<label for="'.$element->getName().'" class="am-u-sm-3 am-form-label">'
				.$element->getLabel()
				.'</label>'
				.'<div class="am-u-sm-9">'
				.$element
				.'</div>';

			if(count($messages)) {
				$input .= '<div class="am-alert-flash am-u-sm-9">';
				foreach($messages as $message) {
					$input .= $this->flash->error($message);
				}
				$input .= '</div>';
			}
			$input .= '</div>';
		} else {
			$input = $element;
		}

		echo $input;
	}
}
